set more off
clear all


local save_figures_flag=1

preserve
use prices_by_region, clear
keep if region=="USA"
keep year price_total
rename price_total price_CPI
keep if year>=1960
save TEMP_US_CPI, replace
restore

*****************************************************
/*main categories of US-wide recreation, current*/
*****************************************************

import delimited "cu_US_recreation.txt", clear 

keep if year<2019

drop footnote_codes
drop if period=="M13"

gen price_item = substr(series_id, 9, 10)
replace price_item  = subinstr(price_item, " ", "", .)


/*
SAR - total recreation
SERA - video and audio (TVs, cable services, other video eq, video discs with rentals, audio equipment, recorded music and subscriptions)
	SERAC - products, SERAS - services
SERB - pets (use SERB01 to be consistent with mu data)
SERC - sport
SERD - photo (use SERD01 to be consistent with mu data)
SERE - other recreation goods (toys, sewing, music) (use SERE01 to be consistent with mu)
SERF - other recreation services (club memberships, admissions, fees for lessons)
SERG - recreational reading (newspapers and magazines, recreational books)

There are more details categories, see cu_items for details.
SARC - recreation commodities
SARS - recreation services
*/
keep if price_item=="SAR" || price_item=="SERA" || price_item=="SERB" || price_item=="SERC" /*
	*/ || price_item=="SERD" || price_item=="SERE" || price_item=="SERF" || price_item=="SERG" /*
	*/ || price_item=="SERA02" || price_item=="SERE01" || price_item=="SERE02"  || price_item=="SERE03" || price_item=="SERA02" /*
	*/ || price_item=="SERB01" || price_item=="SERD01" 
	
		
gen series_type = substr(series_id, 1, 4)
gen area = substr(series_id, 5, 4)
keep if area=="0000"


sort price_item series_type year period
by price_item series_type year: egen price=mean(value)
by price_item series_type year: egen number=count(value)
drop if number==1 & period=="M12" & value!=100 /*drop observations with only 1 december observation per year unless it is 100 (starting value)*/
drop value number

quietly by price_item series_type year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup period series_id area


*reshape wide price, i(year price_item) j(series_type) string
*sort price_item year
keep if series_type=="CUUR" /*not seasonally adjusted, longest available*/
drop series_type

save cu_US_recreation_detailed_processed, replace


*****************************************************
/*main categories of US-wide recreation, historical*/
*****************************************************

import delimited "mu_US_recreation.txt", clear 

keep if year<2019

drop footnote_codes
drop if period=="M13"

gen price_item = substr(series_id, 9, 10)
replace price_item  = subinstr(price_item, " ", "", .)

/*
SA6 - total recreation
SE59 - reading (newspapers, magazines, periodicals, books). 
SE60 - sport
SE6101  - toys, music, hobbies
SE6102 - photo
SE6103 - pets
SE62 - services (club memberships, admissions, fees, and others)

SA6=SAR
SE59=SERG
SE60=SERC
SE6101=SERE (sewing)
SE6102=SERD
SE6103=SERB
SE62=SERF

SERA - video and audio, will come from home expenses

There are more detailed categories, see mu_items for details
*/

keep if price_item=="SA6" || price_item=="SE59" || price_item=="SE60" || price_item=="SE6101" /*
	*/  || price_item=="SE6102" || price_item=="SE6103" || price_item=="SE62" || price_item=="SA61"
	
	

replace price_item="SAR" if price_item=="SA6"
replace price_item="SERG" if price_item=="SE59"
replace price_item="SERC" if price_item=="SE60"
replace price_item="SERE" if price_item=="SE6101"
replace price_item="SERD" if price_item=="SE6102"
replace price_item="SERB" if price_item=="SE6103"
replace price_item="SERF" if price_item=="SE62"



gen series_type = substr(series_id, 1, 4)
gen area = substr(series_id, 5, 4)
keep if area=="0000"

sort price_item series_type year period
by price_item series_type year: egen price=mean(value)
by price_item series_type year: egen number=count(value)
drop if number==1 & period=="M12" & value!=100 /*drop observations with only 1 december observation per year unless it is 100 (starting value)*/
drop value number


quietly by price_item series_type year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup period series_id area


keep if series_type=="MUUR" /*not seasonally adjusted, longest available*/
drop series_type

*commodities/services/overall index back to 1967
preserve
	merge m:1 year using TEMP_US_CPI
	drop _merge
	sort price_item year
	keep if price_item=="SAR" || price_item=="SA61" || price_item=="SERF"
	gen price_real=price/price_CPI
	keep year price_item price_real
	reshape wide price_real, i(year) j(price_item) string
	***graph of real recreation prices over time
	gen byte tag=1 if year==1980
	sort tag
	local list SAR SA61 SERF
	foreach var of local list {
		gen norm_price_real`var'=price_real`var'/price_real`var'[1]
	}
	drop tag
	sort year
	label variable norm_price_realSAR "Total"
	label variable norm_price_realSA61 "Commodities"
	label variable norm_price_realSERF "Services"
	
	
	
	
	twoway  (connected norm_price_realSA61 year, lpattern(solid) msymbol(x) msize(large)  lwidth(0.4)) /*
	*/(connected norm_price_realSERF year, lpattern(dash) msymbol(o) msize(large)  lwidth(0.4)), xline(1980, lcolor(black)) /*
	*/leg(position(11) ring(0) c(1)) xlabel(1967[3]2000) ylabel(0.8[0.1]1.4) xtitle("")/*
	*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmin gmax) ytitle("Price index, 1980=1") ylabel(,grid nogextend gmin gmax) ysize(4)
	if `save_figures_flag'==1{
		graph export "../figures/recreation_price_pre_trend.png", as(png) replace
		graph export "../figures/recreation_price_pre_trend.eps", as(eps) replace
	}
	*black and white
	twoway  (connected norm_price_realSA61 year, lpattern(solid) msymbol(x) msize(large)  lwidth(0.4)) /*
	*/(connected norm_price_realSERF year, lpattern(dash) msymbol(o) msize(large)  lwidth(0.4)), xline(1980, lcolor(black)) /*
	*/leg(position(11) ring(0) c(1)) xlabel(1967[3]2000) ylabel(0.8[0.1]1.4) xtitle("")/*
	*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmin gmax) ytitle("Price index, 1980=1") ylabel(,grid nogextend gmin gmax) ysize(4) scheme(s1mono)
	if `save_figures_flag'==1{
		graph export "../figures/recreation_price_pre_trend_bw.png", as(png) replace
		graph export "../figures/recreation_price_pre_trend_bw.eps", as(eps) replace
	}
restore


save mu_US_recreation_detailed_processed, replace



*********************************************************************************************



/*get audio-video prices from housing*/
import delimited "mu_US_housing.txt", clear 
keep if year<2019

drop footnote_codes
drop if period=="M13"

gen price_item = substr(series_id, 9, 10)
replace price_item  = subinstr(price_item, " ", "", .)


/*
SE31 - video and audio products
SE2703 - cable TV
There are more details categories, see mu_items for details
*/

keep if price_item=="SE31" || price_item=="SE2703"

gen series_type = substr(series_id, 1, 4)
gen area = substr(series_id, 5, 4)
keep if area=="0000"

sort price_item series_type year period
by price_item series_type year: egen price=mean(value)
by price_item series_type year: egen number=count(value)
drop if number==1 & period=="M12" & value!=100 /*drop observations with only 1 december observation per year unless it is 100 (starting value)*/
drop value number


quietly by price_item series_type year:  gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup period series_id area

keep if series_type=="MUUR" /*not seasonally adjusted, longest available*/
drop series_type

append using mu_US_recreation_detailed_processed

rename price price_mu



merge 1:1 year price_item using cu_US_recreation_detailed_processed
drop _merge
sort price_item year






**PETS**
preserve
/*
keep if price_item=="SERB" || price_item=="SERB01"
gen price_pets=price_mu
replace price_pets=price_pets[_n-1]*price/price[_n-1] if price_pets==.
*/
keep if price_item=="SERB01"
rename price price_pets
keep year price_pets
save TEMP_price_pets, replace
restore




**SPORT**
preserve
keep if price_item=="SERC"
rename price price_sport
keep year price_sport
save TEMP_price_sport, replace
restore

**PHOTO**
preserve
keep if price_item=="SERD01"
rename price price_photo
keep year price_photo
save TEMP_price_photo, replace
restore


**READING**
preserve
keep if price_item=="SERG"
rename price price_reading
keep year price_reading
save TEMP_price_reading, replace
restore


**OTHER SERVICES**
preserve
keep if price_item=="SERF"
gen price_oth_serv=price_mu
replace price_oth_serv=price_oth_serv[_n-1]*price/price[_n-1] if price_oth_serv==.
keep year price_oth_serv
save TEMP_price_oth_serv, replace
restore


**OTHER GOODS**
preserve
keep if price_item=="SERE01"
rename price price_oth_goods
keep year price_oth_goods
save TEMP_price_oth_goods, replace
restore


**AUDIO-VIDEO**
preserve
keep if price_item=="SE31" || price_item=="SE2703" || price_item=="SERA" || price_item=="SERA02" 
replace price=price_mu if price_item!="SERA" & price_item!="SERA02"
drop price_mu
reshape wide price, i(year) j(price_item) string
merge 1:1 year using "../CEX_data/rec_cex.dta"
drop _merge
keep year price* rec_cabel rec_audio_video
gen price_c=.
replace price_c=100 if year==1980
gen rec_audio_video_no_cabel=rec_audio_video-rec_cabel
/*ignore price of cable for 1980-1982 because cons data is not available*/
replace price_c=price_c[_n-1]*priceSE31/priceSE31[_n-1] if priceSE2703[_n-1]==. & price_c==.
replace price_c=price_c[_n-1]*(priceSE31/priceSE31[_n-1]*rec_audio_video_no_cabel+priceSE2703/priceSE2703[_n-1]*rec_cabel)/rec_audio_video if priceSE2703[_n-1]!=. & price_c==.
gen price_audio_video=.
replace price_audio_video=price_c
replace price_audio_video=price_audio_video[_n-1]*priceSERA/priceSERA[_n-1] if price_audio_video==.
keep year price_audio_video
save TEMP_price_audio_video, replace
restore


**TOTAL RECREATION***
preserve
keep if price_item=="SAR"
gen price_rec_sp=price_mu
replace price_rec_sp=price_rec_sp[_n-1]*price/price[_n-1] if price[_n-1]!=.
keep year price_rec_sp
label variable price_rec_sp "Smoothly pasted entertainment and recreation index"
save TEMP_price_rec_sp, replace
restore


********************************************************************************

clear all
erase mu_US_recreation_detailed_processed.dta
use TEMP_US_CPI, clear
keep if year>=1960
sort year

local list pets sport reading oth_serv oth_goods photo audio_video rec_sp
foreach var of local list{
	merge 1:1 year using TEMP_price_`var'
	drop _merge
	sort year
	erase TEMP_price_`var'.dta
}
merge 1:1 year using "../CEX_data/rec_cex.dta"
drop _merge




keep if year>=1980


gen price_rec_weighted=.
replace price_rec_weighted=100 if year==1980
gen aux_rec_no_audio_video=cons_rec-rec_audio_video
*change recreation price by adding audio_video pre-1993
replace price_rec_weighted=price_rec_weighted[_n-1]*(price_audio_video/price_audio_video[_n-1]*rec_audio_video+price_rec_sp/price_rec_sp[_n-1]*aux_rec_no_audio_video)/cons_rec if year<=1993 & year>=1981
*after 1993, use recreation price index which includes audio-video
replace price_rec_weighted=price_rec_weighted[_n-1]*price_rec_sp/price_rec_sp[_n-1] if year>1993

drop aux*
local list pets sport reading oth_serv oth_goods photo audio_video rec_sp rec_weighted

foreach var of local list{
	replace price_`var'=price_`var'/price_CPI
}


gen byte tag=1 if year==2015
sort tag
foreach var of local list {
	gen price_`var'_r=100*price_`var'/price_`var'[1]
	drop price_`var'
}
drop tag
sort year
keep if year>=1980
keep year price*

save prices_US_rec_detailed, replace
erase TEMP_US_CPI.dta
erase cu_US_recreation_detailed_processed.dta



***graph of real recreation prices over time
gen byte tag=1 if year==1980
sort tag
foreach var of local list {
	gen norm_price_`var'=1*price_`var'_r/price_`var'_r[1]
}
drop tag
sort year

drop norm_price_rec_sp

label variable norm_price_pets "Pets"
label variable norm_price_reading "Reading"
label variable norm_price_sport "Sports"
label variable norm_price_photo "Photo"
label variable norm_price_audio_video "Audio and video"
label variable norm_price_oth_serv "Other services"
label variable norm_price_oth_goods "Other goods"
label variable norm_price_rec_weighted "Total"

rename norm_price_rec_weighted Xnorm_price_rec_weighted



twoway  (line Xnorm_price_rec_weighted year, lwidth(thick) lcolor(black)) (connected norm_price* year,/*
*/lpattern(dash dash dash_dot shortdash solid longdash solid solid) /*
*/msymbol(x p p p s p x o)), /*
*/leg(position(11) ring(0)) xlabel(1980[5]2020) ylabel(0[0.2]1.8) xtitle("")/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmin gmax) ytitle("Price index, 1980=1") ylabel(,grid nogextend gmin gmax) ysize(4)
if `save_figures_flag'==1{
	graph export "../figures/recreation_price_by_category_US.eps", as(eps) replace
	graph export "../figures/recreation_price_by_category_US.png", as(png) replace
}
*black and white
twoway  (line Xnorm_price_rec_weighted year, lwidth(thick) lcolor(black)) (connected norm_price* year,/*
*/lpattern(dash dash dash_dot shortdash solid longdash solid solid) /*
*/msymbol(x p p p s p x o)), /*
*/leg(position(11) ring(0)) xlabel(1980[5]2020) ylabel(0[0.2]1.8) xtitle("")/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmin gmax) ytitle("Price index, 1980=1") ylabel(,grid nogextend gmin gmax) ysize(4) scheme(s1mono)
if `save_figures_flag'==1{
	graph export "../figures/recreation_price_by_category_US_bw.eps", as(eps) replace
	graph export "../figures/recreation_price_by_category_US_bw.png", as(png) replace
}
